//
//  MamberselectViewController.m
//  BiologyLibrary
//
//  Created by milo on 16/7/3.
//  Copyright © 2016年 youxh. All rights reserved.
//

#import "MamberselectViewController.h"
#import "menberselectCell.h"
#import "ServiceUser.h"
@interface MamberselectViewController ()

@end

@implementation MamberselectViewController
@synthesize delegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviBarTitle:@"选择"];
//    // 创建一个自定义的按钮，并添加到导航条右侧。
//    UIButton*_btnNaviLeft = [CustomNaviBarView
//                              createNormalNaviBarBtnByTitle:@"全选"
//                              target:self
//                              action:@selector(selectAll)];
    [self setNaviBarLeftBtn:nil];
    // 创建一个自定义的按钮，并添加到导航条右侧。
    UIButton*_btnNaviRight = [CustomNaviBarView
                             createNormalNaviBarBtnByTitle:@"确定"
                             target:self
                             action:@selector(submit)];
    [self setNaviBarRightBtn:_btnNaviRight];

    menberlistView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, fWidth_Current, fHeight_Current-64)];
    menberlistView.backgroundColor = [UIColor clearColor];
    menberlistView.separatorStyle = UITableViewCellSeparatorStyleNone;
    menberlistView.dataSource = self;
    menberlistView.delegate = self;
    menberlistView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:menberlistView];
    menberArr=[[NSMutableArray alloc]init];
    selectarr=[[NSMutableArray alloc]init];
    selectmenberArr=[[NSMutableArray alloc]init];
    
    menberArr = [NSMutableArray arrayWithArray:[ServiceUser findAll]];
    
    if ([_type isEqualToString:@"1"]) {
        for (int i=0; i<menberArr.count; i++) {
            ServiceUser *tempUser = (ServiceUser *)[menberArr objectAtIndex:i];
            if (![tempUser.authority isEqualToString:@"0"]) {
                [menberArr removeObjectAtIndex:i];
            }
            
        }

    }
        [selectarr removeAllObjects];
    for (int i=0; i<menberArr.count; i++) {
        [selectarr addObject:@"0"];
    }

}
-(void)submit{
    if (selectmenberArr.count==0) {
        [self.view makeToast:@"请至少选择一位人员" position:@"center"];
        return;
    }
    if ([self.delegate respondsToSelector:@selector(Manberselectfinished:withmenberarr:withIndex:)]){
        
        [self.delegate Manberselectfinished:self withmenberarr:selectmenberArr withIndex:_nowIndex];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
#pragma mark - UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return [menberArr count];
        
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   
        return 1 ;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"CustomCell";
    menberselectCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"menberselectCell" owner:self options:nil];
    
    for (id oneObject in nib)
    {
        if ([oneObject isKindOfClass:[menberselectCell class]])
        {
            cell = (menberselectCell *)oneObject;
        }
    }
    cell.contentView.backgroundColor=cellcolor;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    float fHeight_Row = [self tableView:tableView heightForRowAtIndexPath:indexPath];
    
    UIImageView*lineview=[[UIImageView alloc]initWithFrame:CGRectMake(0,fHeight_Row , fWidth_Current, 0.5)];
    lineview.backgroundColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
    [cell addSubview:lineview];
    
    ServiceUser *tempUser = (ServiceUser *)[menberArr objectAtIndex:indexPath.row];
    
    cell.picimage.image=[UIImage imageNamed:@"userhead_temp"];
    cell.picimage.layer.cornerRadius=19;
    cell.picimage.clipsToBounds=YES;
    
    cell.namelabel.text=tempUser.displayname;
    [cell.editbutton setImage:[UIImage imageNamed:@"selectbtn"] forState:UIControlStateNormal];
    [cell.editbutton setImage:[UIImage imageNamed:@"selectbtn_f"] forState:UIControlStateSelected];
    [cell.editbutton addTarget:self action:@selector(checkBtnClick:)
              forControlEvents:UIControlEventTouchUpInside];
    [cell.editbutton setTag:indexPath.row];
    NSString*str=[selectarr objectAtIndex:indexPath.row];
    if ([str isEqualToString:@"1"]) {
        [cell.editbutton setSelected:YES];
    }
    
    return cell;
}

-(void)checkBtnClick:(UIButton *)sender{
    [sender setSelected:!sender.isSelected];
    [selectarr removeAllObjects];
    for (int i=0; i<menberArr.count; i++) {
        [selectarr addObject:@"0"];
    }
    [selectmenberArr removeAllObjects];
        if (sender.selected==YES) {
            [selectarr replaceObjectAtIndex:sender.tag withObject:@"1"];
            [selectmenberArr addObject:[menberArr objectAtIndex:sender.tag]];
        }else{
            [selectarr replaceObjectAtIndex:sender.tag withObject:@"0"];
            
        }
//        if (selectmenberArr.count==menberArr.count) {
//            UIButton*_btnNaviLeft = [CustomNaviBarView
//                                      createNormalNaviBarBtnByTitle:@"全不选"
//                                      target:self
//                                      action:@selector(deselectAll)];
//            [self setNaviBarLeftBtn:_btnNaviLeft];
//            
//        }else{
//            UIButton*_btnNaviLeft = [CustomNaviBarView
//                                      createNormalNaviBarBtnByTitle:@"全选"
//                                      target:self
//                                      action:@selector(selectAll)];
//            [self setNaviBarLeftBtn:_btnNaviLeft];
//            
//        }
    
    NSLog(@"%@",selectmenberArr);
    [menberlistView reloadData];
    
}

//-(void)selectAll{
//    // 创建一个自定义的按钮，并添加到导航条右侧。
//    UIButton*_btnNaviLeft = [CustomNaviBarView
//                              createNormalNaviBarBtnByTitle:@"全不选"
//                              target:self
//                              action:@selector(deselectAll)];
//    [self setNaviBarLeftBtn:_btnNaviLeft];
//    [selectmenberArr removeAllObjects];
//        [selectarr removeAllObjects];
//        for (int i=0; i<[menberArr count]; i++) {
//            [selectarr addObject:@"1"];
//            [selectmenberArr addObject:[menberArr objectAtIndex:i]];
//        }
//        
//        
//    NSLog(@"%@",selectmenberArr);
//    [menberlistView reloadData];
//}
//-(void)deselectAll{
//    // 创建一个自定义的按钮，并添加到导航条右侧。
//    UIButton*_btnNaviLeft = [CustomNaviBarView
//                              createNormalNaviBarBtnByTitle:@"全选"
//                              target:self
//                              action:@selector(selectAll)];
//    [self setNaviBarLeftBtn:_btnNaviLeft];
//    [selectarr removeAllObjects];
//    [selectmenberArr removeAllObjects];
//    for (int i=0; i<[menberArr count]; i++) {
//        [selectarr addObject:@"0"];
//    }
//    
//    NSLog(@"%@",selectmenberArr);
//    
//    [menberlistView reloadData];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
